﻿@model Masuit.MyBlogs.Core.Models.Pagination
@using Masuit.MyBlogs.Domain.Entity
@using Masuit.MyBlogs.Domain.Enum
@using Masuit.MyBlogs.Services.DTO
@using Masuit.Tools.Helper
@using Masuit.Tools
@{
    int pages = Math.Ceiling(Model.TotalCount / (Model.Size > 0 ? Model.Size : 15.0)).ToInt32();
    int current = Model.Page > 0 ? Model.Page : 1;
    int pageStart = current - 4 > 0 ? current - 4 : 1;
    int pageEnd = current + 4 >= pages ? pages : current + 4;
    current = current > pages ? pages : current;
    current = current < 1 ? 1 : current;
    if (pageEnd - pageStart < 9)
    {
        if (pageStart == 1)
        {
            pageEnd = pageStart + 8 >= pages ? pages : pageStart + 8;
        }
        else
        {
            pageStart = pageEnd - 8 > 0 ? pageEnd - 8 : 1;
        }
    }
}
@if (pages > 1)
{
    <ul class="pagination" style="margin: 0">
        @if (current == 1)
        {
            <li class="disabled">
                <a>
                    首页
                </a>
            </li>
            <li class="disabled">
                <a aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        }
        else
        {
            <li>
                <a href="@($"{Context.Request.Path}?orderby={Model.OrderBy}".TrimQuery())" aria-label="Previous">
                    首页
                </a>
            </li>
            <li>
                <a href="@($"{Context.Request.Path}?page={(Model.Page - 1 <= 0 ? 1 : Model.Page - 1)}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        }
        @if (pageStart > 1)
        {
            <li class="disabled">
                <a aria-label="Previous">
                    <span aria-hidden="true">...</span>
                </a>
            </li>
        }
        @for (int i = pageStart; i <= pageEnd; i++)
        {
            if (i == current)
            {
                <li class="disabled">
                    <a>@i</a>
                </li>
            }
            else
            {
                <li>
                    <a href="@($"{Context.Request.Path}?page={i}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())">@i</a>
                </li>
            }
        }
        @if (pageEnd < pages)
        {
            <li class="disabled">
                <a aria-label="Previous">
                    <span aria-hidden="true">...</span>
                </a>
            </li>
        }
        @if (current == pages)
        {
            <li class="disabled">
                <a>
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            <li class="disabled">
                <a>
                    末页
                </a>
            </li>
        }
        else
        {
            <li>
                <a href="@($"{Context.Request.Path}?page={current + 1}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            <li>
                <a href="@($"{Context.Request.Path}?page={pages}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())" aria-label="Next">
                    末页
                </a>
            </li>
        }
    </ul>
}